<template>
    <div class="container">
        <div class="top_1">
            <div class="left">
                <a @click="routerback"><img src="../assets/img/back.png"><span>分类</span></a>
            </div>
            <div class="right">
                <router-link to="/index"><span>书城</span><img src="../assets/img/home.png"></router-link>
            </div>
        </div>
        <div class="top">
            <!--分类-->
            <div class="fenlei">
                <div class="all" @click="search_all()" :class="search.sex===0?'dianji':''">全部</div>
                <div class="boy" @click="search_sex(1)" :class="search.sex===1?'dianji':''">男生频道</div>
                <div class="girl" @click="search_sex(2)" :class="search.sex===2?'dianji':''">女生频道</div>
            </div>
            <div class="classify">
                <div @click="search_sub()" :class="search.tag_name===''?'dianji':''">全部</div>
                <div v-for="tag in show_tags" :key="tag.ID" @click=search_tag(tag.Name) :class="search.tag_name===tag.Name?'dianji':''">{{tag.Name}}</div>
            </div>
            <!--选项-->
            <div class="state">
                <div @click="search_finished(0)" :class="search.finished===0?'dianji':''">全部</div>
                <div @click="search_finished(2)" :class="search.finished===2?'dianji':''">连载中</div>
                <div @click="search_finished(1)" :class="search.finished===1?'dianji':''">已完结</div>
            </div>
        </div>
        <div class="content">
            <!--书籍显示-->
            <div class="intro" v-for="book in books" :key="book.ID">
                <router-link :to="{path:'/book_details',query:{id:book.ID}}">
                    <div class="intro_tu">
                        <img v-lazy="book.ImageUrl">
                    </div>
                    <div class="intro_right">
                        <p class="intro_title">{{book.Title}}</p>
                        <p class="intro_intro">{{book.Description}}</p>
                        <span class="intro_fenlei">{{book.TagName}}</span>
                    </div>
                </router-link>
            </div>
        </div>
        <div class="meiyou"  v-if="none">没有更多图书了</div>
        <div class="see_more" v-if="more" @click="next_serach">
            点击查看更多
        </div>
    </div>
</template>

<script>
    export default {
        name: "classify_search",
        data(){
            return{
                search: {
                    title:"",
                    offset: 0,
                    limit: 15,
                    finished: 0, // 0 不选择 1 已完结 2 未完结
                    sex: 0, // 0 不选择 1 男生 2 女生
                    tag_name: '', // 标签 名称
                },
                tags: [], // 所有的分类列表
                show_tags: [], // 显示分类列表
                books: [], // 显示的图书列表
                none: false,
                more: false,
            }
        },
        created: function() {
            this.$axios.get('book-tags').then(response=>{
                this.tags = response.data.tags
                this.show_tags = response.data.tags
            })
            this.start_search();
            this.set()
            document.title = '分类';
        },
        methods: {
            set:function(){
                this.$emit('set','hide')
            },
            routerback: function () {
                this.$router.back(-1)
            },
            // 开始搜索
            start_search: function(push) {
                this.$axios.get('/books',{params: {PtID:0,ForbidView:2,Finished:this.search.finished,Sex:this.search.sex,Limit:this.search.limit,Offset:this.search.offset,TagName:this.search.tag_name,Descending:false}}).then(response=>{
                    if(response) {
                        if(response.data.books.length < this.search.limit){
                            this.none = true
                            this.more = false
                        }else{
                            this.none = false
                            this.more = true
                        }
                        if(push) {
                            for(let i = 0, ln = response.data.books.length; i < ln; i++) {
                                this.books.push(response.data.books[i])
                            }
                        } else {
                            this.books = response.data.books;
                        }
                    } else {
                        if(!push){
                            this.books = []
                        }
                        this.none = true
                        this.more = false
                    }
                })
            },
            search_finished: function(finished) {
                this.search.finished = finished;
                this.search.offset=0
                this.start_search();
            },
            search_all: function() {
                this.search = {
                    offset: 0,
                    limit: 15,
                    finished: 0, // 0 不选择 1 已完结 2 未完结
                    sex: 0, // 0 不选择 1 男生 2 女生
                    tag_name: '', // 标签 ID
                }
                this.show_tags = this.tags;
                this.start_search();
            },
            search_sub: function() {
                this.search = {
                    offset: 0,
                    limit: 15,
                    finished: 0,
                    sex: this.search.sex,
                    tag_name: '',
                }
                this.start_search();
            },
            search_tag: function(tag_name) {
                this.search.tag_name = tag_name;
                this.search.offset=0
                this.start_search();
            },
            search_sex: function(sex) {
                this.search.tag_name = '';
                this.search.offset=0
                this.search.sex = sex;
                this.show_tags = [];
                for(let i = 0, ln = this.tags.length; i < ln; i++) {
                    let tag = this.tags[i];
                    if(tag.Sex === sex) {
                        this.show_tags.push(tag)
                    }
                }
                this.start_search()
            },
            next_serach: function() {
                this.search.offset += this.search.limit;
                this.start_search(true);
            }
        }
    }
</script>

<style scoped>
    .container {
        background: white;
        margin-bottom: 20px;
        padding: 0px;
    }
    .meiyou {
        text-align: center;
        padding: 20px 0px 5px;
        color: #999;
    }
    .top {
        padding: 0px 10px;
    }
    .top_1 {
        padding: 10px 0px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        background: #ebebeb;
        border-bottom: 1px solid gainsboro;
    }

    .left a {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
    }

    .right a {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
    }

    .left a,
    .right a {
        color: black;
        text-decoration: none;
    }

    .top_1 img {
        width: 2rem;
    }

    .top_1 span {
        font-size: 1.1rem;
    }
    .header {
        margin: 20px 8px;
        padding: 2px 0px;
        display: flex;
        flex: 1;
        flex-direction: row;
        border-radius: 22px;
        background: rgba(238,238,238,0.7);
        /*position: sticky;*/
        top:0px;
        z-index: 100;
        overflow: hidden;
    }
    .header2{
        position: fixed;
        top: 0px;
        width: 95%;
        margin: 0px 10px;
    }

    .header img {
        width: 3rem;
        height: 3rem;
        border: 0;
    }
    form{
        display: flex;
        align-items: center;
        width: 85%;
    }
    .header input {
        border: none;
        outline: none;
        margin-left: 5px;
        -webkit-appearance: none;
        background: none;
        color: black;
        width: 100%;
    }

    .classify,.state,.fenlei{
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        border-bottom:1px solid gainsboro;
        margin-top: 10px;
    }
    .classify div,.state div,.fenlei div{
        width: 7rem;
        text-align: center;
        padding: 3px 0px;
        margin-bottom: 10px;
    }
    .tishi{
        text-align: center;
        margin: 10px 0px;
        color:#C1C1C1 ;
    }
    .state{
        padding-top: 10px;
    }
    .dianji {
        border-radius: 12px;
        /*background: #3688ff;*/
        color: #3688ff;
    }
    .fenlei{
        padding: 10px 0px;
        border-bottom:1px solid gainsboro;
    }
    .fenlei div{
        margin-bottom: 0px;
    }
    .option {
        display: flex;
        justify-content: flex-start;
        margin: 10px 0px;
        border-bottom:1px solid gainsboro;
    }

    .option>div {
        width: 50%;
        border-bottom:none;
    }

    .option>div>div {
        width: 7rem;
    }

    .intro {
        margin-top: 20px;
    }

    .intro a{
        display: flex;
        text-decoration: none;
        color: black;
    }

    .intro img {
        width: 6.5rem;
        height: 9.5rem;
        margin-left: 15%;
        -webkit-box-shadow: 0 4px 10px rgba(0,0,0,.2);
        box-shadow: 0 4px 10px rgba(0,0,0,.2);
    }

    .intro_right {
        margin-left: 30px;
        margin-right: 15px;
    }

    .intro_title {
        font-size: 1.2rem;
        margin-top: 2px;
    }

    .intro_writer {
        color: #c1c1c1;
        font-size: 10px;
    }

    .intro_intro {
        margin-top: 10px;
        color: #999;
        font-size: 1rem;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .intro_fenlei {
        text-align: center;
        font-size: 1rem;
        color: #989898;
    }

    .see_more {
        margin: 20px 10px;
        margin-bottom: 10px;
        padding: 10px 0px;
        background: #3688ff;
        border-radius: 5px;
        text-align: center;
        color: white;
        font-size: 16px;
    }
</style>
